import { states } from "./states";
import { checkobjectkey } from "./checkobjectkeys";
export default async function loadprerenderpage(pageurl: string) {
    if (process.env.PRERENDER) {
        if (states.menulist.includes(pageurl)) {
            //@ts-ignore
            const statesdatamap = (await import("../temp/states.js"))
                .loadpagedatamap;

            checkobjectkey(statesdatamap, pageurl, "function");

            const pagedata = await statesdatamap[pageurl]();

            checkobjectkey(pagedata, "url", "string");
            checkobjectkey(pagedata, "markdown", "string");
            checkobjectkey(pagedata, "toc", "string");
            checkobjectkey(pagedata, "title", "string");
            const { markdown, toc, title } = pagedata;
            if (pagedata.url === pageurl) {
                states.cachemain.get(pageurl) ||
                    states.cachemain.set(pageurl, markdown);
                states.cachetoc.get(pageurl) ||
                    states.cachetoc.set(pageurl, toc);
                states.cachetitle.get(pageurl) ||
                    states.cachetitle.set(pageurl, title);
            } else {
                throw new Error("page data and url not match");
            }
        }
    }
}
